﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace JJY.CommonHelper
{
    public class TableCreater
    {
        public static string Create(DataTable dataTable, string tableName)
        {
            StringBuilder result = new StringBuilder();

            result.AppendFormat(@"<table id=""{0}"">", tableName);
            
            result.AppendFormat("<thead><tr>");
            foreach (DataColumn dc in dataTable.Columns)
            {
                result.AppendFormat("<th>{0}</th>",dc.ColumnName);
            }
            result.AppendFormat("</tr></thead>");

            result.AppendFormat("<tbody>");
            foreach (DataRow dr in dataTable.Rows)
            {
                result.AppendFormat("<tr>");
                foreach (DataColumn dc in dataTable.Columns)
                {
                    result.AppendFormat("<td>{0}</td>", dr[dc.ColumnName].ToString());
                }
                result.AppendFormat("</tr>");
            }
            result.AppendFormat("</tbody>");

            result.AppendFormat("</table>");

            return result.ToString();
        }
    }
}
